package com.wsapi.module.role.controller;

import com.wsapi.commons.vo.Page;
import com.wsapi.commons.vo.PageList;
import com.wsapi.commons.vo.TreeNode;
import com.wsapi.core.Module;
import com.wsapi.core.Platform;
import com.wsapi.core.Platforms;
import com.wsapi.core.dao.model.SysRole;
import com.wsapi.core.dao.model.SysRolePerm;
import com.wsapi.module.role.service.RoleManagementService;
import java.util.ArrayList;
import java.util.Collections;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * 角色管理 User: yangyan Date: 14-1-2 Time: 下午10:04
 */
@Controller
@RequestMapping("/management/role")
public class RoleManagementController {

    @Resource
    private RoleManagementService roleManagementService;

    /**
     * 列表
     *
     * @param offset
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(Page.Offset offset) {
        Map<String, Object> map = new HashMap<>();
        PageList<SysRole> roleList = roleManagementService.findByPage(offset);
        map.put("success", true);
        map.put("roleList", roleList);
        return map;
    }

    /**
     * 添加
     *
     * @param role
     * @param perms
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    @RequiresPermissions("ROLE:ADD")
    public Map<String, Object> add(SysRole role, String[] perms) {
        Map<String, Object> map = new HashMap<>();

        role.setPermissions(new HashSet<SysRolePerm>());

        if (perms != null) {
            for (String perm : perms) {
                SysRolePerm rp = new SysRolePerm();
                rp.setPermKey(perm);
                rp.setRole(role);
                role.getPermissions().add(rp);
            }
        }

        roleManagementService.addRole(role);
        map.put("success", true);

        return map;
    }

    /**
     * 查询权限树，如果给定了角色ID,返回的结果中刺角色具备的权限checked 是 true
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/perm_treenodes")
    @ResponseBody
    @RequiresAuthentication
    public List<TreeNode> permTreeNodes(Integer roleId) {

        Set<String> rolePerms = null;
        if (roleId != null && roleId > 0) {
            rolePerms = this.roleManagementService.getPermissionsByRoleId(roleId);
        }

        if (rolePerms == null) {
            rolePerms = Collections.EMPTY_SET;
        }

        List<Platform> platforms = Platforms.getPlatforms();
        List<TreeNode> nodes = new ArrayList<>();
        for (Platform p : platforms) {
            TreeNode n = new TreeNode();
            n.setText(p.getName());
            for (Module m : p.getModules()) {
                TreeNode n2 = new TreeNode();
                n2.setText(m.getName());
                for (Module.ModulePermission mp : m.getPermissionList()) {
                    TreeNode n3 = new TreeNode();
                    n3.setText(mp.getName());
                    n3.setValue(m.getId() + ":" + mp.getKey());

                    if (rolePerms.contains(m.getId() + ":" + mp.getKey())) {
                        n3.setChecked(true);
                    } else {
                        n3.setChecked(false);
                    }

                    n3.setLeaf(true);
                    n2.addChildren(n3);
                }
                if (n2.getChildren() != null) {
                    n2.setExpanded(true);
                    n2.setLeaf(false);
                } else {
                    n2.setLeaf(true);
                }
                n.addChildren(n2);
            }

            if (n.getChildren() != null) {
                n.setExpanded(true);
                n.setLeaf(false);
            } else {
                n.setLeaf(true);
            }
            nodes.add(n);
        }
        return nodes;
    }

    /**
     * 修改
     *
     * @param role
     * @param perms
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions("ROLE:EDIT")
    public Map<String, Object> update(SysRole role, String[] perms) {
        Map<String, Object> map = new HashMap<>();

        role.setPermissions(new HashSet<SysRolePerm>());
        for (String perm : perms) {
            SysRolePerm rp = new SysRolePerm();
            rp.setPermKey(perm);
            rp.setRole(role);
            role.getPermissions().add(rp);
        }
        roleManagementService.updateRole(role);
        map.put("success", true);
        return map;
    }

    /**
     * 删除
     *
     * @param role
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    @RequiresPermissions("ROLE:REMOVE")
    public Map<String, Object> remove(SysRole role) {
        Map<String, Object> map = new HashMap<>();

        roleManagementService.removeRole(role);
        map.put("success", true);

        return map;
    }

}
